class Conditions:
	conditions = []

	def __init__(self):
		pass

	def addCondition(self, tag, condition, index=-1):
		newCondition = {
			'tag': tag,
			'value': condition
		}
		if index > -1 and not self.haveCondition(tag):
			self.conditions[index] = newCondition
		else:
			targetIndex = self.getIndex(tag)
			if targetIndex > -1:
				self.conditions[targetIndex] = newCondition
			else:
				self.conditions.append(newCondition)

	def removeCondition(self, tag='', index=-1):
		if tag == '' and -1 < index < len(self.conditions):
			self.conditions.pop(index)
			return
		for (i, condition) in enumerate(self.conditions):
			if condition['tag'] == tag:
				self.conditions.pop(i)
				return
		print('Remove failed. No such tag that named {} was found.'.format(tag))

	def clearAllConditions(self):
		self.conditions.clear()

	def getIndex(self, tag):
		for (i, condition) in enumerate(self.conditions):
			if condition['tag'] == tag:
				return i
		return -1

	def isAllTrue(self):
		for condition in self.conditions:
			if not condition['value']:
				return False
		return True

	def isAllFalse(self):
		for condition in self.conditions:
			if condition['value']:
				return False
		return True

	def haveTrue(self, tags=None, indexes=None):
		if tags is None and indexes is None:
			return not self.isAllFalse()
		for (index, condition) in enumerate(self.conditions):
			print(index, condition)
			for tag in tags:
				if condition['tag'] == tag and condition['value']:
					return True
			for i in indexes:
				if i == index and condition['value']:
					return True
		return False

	def areTrue(self, tags=None, indexes=None):
		if tags is None and indexes is None:
			return self.isAllFalse()
		for (index, condition) in enumerate(self.conditions):
			if tags is not None:
				for tag in tags:
					if condition['tag'] == tag and not condition['value']:
						return False
			if indexes is not None:
				for i in indexes:
					if i == index and not condition['value']:
						return False
		return True

	def isTrue(self, tag='', index=-1):
		for (i, condition) in enumerate(self.conditions):
			if condition['tag'] == tag:
				return condition['value']
			if index == i:
				return condition['value']
		return None

	def haveCondition(self, tag):
		return self.getIndex(tag) != -1


if __name__ == '__main__':
	conditions = Conditions()
	conditions.addCondition('c1', True)
	conditions.addCondition('c2', False)
	conditions.addCondition('c3', False)
	conditions.addCondition('c4', True)
	print('isAllTrue(): {}'.format(conditions.isAllTrue()))
	print('isAllFalse(): {}'.format(conditions.isAllFalse()))
	for (i, c) in enumerate(conditions.conditions):
		print('isTrue(): {}'.format(conditions.isTrue('c{}'.format(i + 1))))
	print('areTrue(): {}'.format(conditions.areTrue(tags=['c1', 'c4'])))
	print('areTrue(): {}'.format(conditions.areTrue(tags=['c1', 'c2'])))
	print('areTrue(): {}'.format(conditions.areTrue(tags=['c2', 'c3'])))
	print('^^^^ areTrue() by tags ^^^^')
	print('areTrue(): {}'.format(conditions.areTrue(indexes=[0, 3])))
	print('areTrue(): {}'.format(conditions.areTrue(indexes=[0, 1])))
	print('areTrue(): {}'.format(conditions.areTrue(indexes=[1, 2])))
	conditions.removeCondition(index=0)
	conditions.removeCondition(tag='c2')
	print(conditions.conditions)
	conditions.addCondition('c3', True)
	conditions.addCondition('new C4', True, index=1)
	print(conditions.conditions)
	print('isAllTrue(): {}'.format(conditions.isAllTrue()))
	print('isAllFalse(): {}'.format(conditions.isAllFalse()))
	conditions.addCondition('c2', False, index=0)
	conditions.addCondition('c3', False, index=1)
	print('isAllTrue(): {}'.format(conditions.isAllTrue()))
	print('isAllFalse(): {}'.format(conditions.isAllFalse()))

